﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;

using EFProviderWrapperToolkit;
using EFTracingProvider;
using EFCachingProvider.Caching;
using EFCachingProvider;
using log4net;
using log4net.Config;



namespace Query
{

    public class GetFromDBService : IGetFromDBService
    {
        private static readonly ILog Log4net = LogManager.GetLogger(typeof(GetFromDBService));
        public List<Rate> RateList = new List<Rate>();
        static bool Config = true;

        private List<Rate> ChartDay(string Day_Temp, string CurrencyName)
        {
            RateList.Clear();
            if (Day_Temp == null)
                return null;
            else
            {
                DateTime DateBegin = Convert.ToDateTime(Day_Temp);
                DateTime DateEnd = Convert.ToDateTime(Day_Temp).AddDays(1);
                using (ExtRatesEntities context = new ExtRatesEntities())
                {
                    Int32 CurrencyId = context.Currency.Where(c => c.Name == CurrencyName).FirstOrDefault().ID;
                    foreach (var Rate_Day in context.cache_Day.Where(c => c.CurrencyID == CurrencyId).Where(c => c.Period >= DateBegin && c.Period < DateEnd))
                    {
                        Rate Answer_Temp = new Rate(Rate_Day.Period, Rate_Day.Value);
                        RateList.Add(Answer_Temp);
                    }
                }
                return RateList;
            }
        }
        public List<Rate> ChartDayXML(string Day_Temp, string CurrencyName)
        {
            return ChartDay(Day_Temp, CurrencyName);
        }
        public List<Rate> ChartDayJson(string Day_Temp, string CurrencyName)
        {
            return ChartDay(Day_Temp, CurrencyName);
        }

        private List<Rate> ChartWeek(string Day_Temp, string CurrencyName)
        {
            RateList.Clear();

            if (Day_Temp == null)
                return null;
            else
            {
                DateTime DateBegin = Convert.ToDateTime(Day_Temp);
                DateTime DateEnd = Convert.ToDateTime(Day_Temp).AddDays(7);
                using (ExtRatesEntities context = new ExtRatesEntities())
                {
                    Int32 CurrencyId = context.Currency.Where(c => c.Name == CurrencyName).FirstOrDefault().ID;
                    foreach (var Rate_Week in context.cache_Week.Where(c => c.CurrencyID == CurrencyId).Where(c => c.Period >= DateBegin && c.Period < DateEnd))
                    {
                        Rate Answer_Temp = new Rate(Rate_Week.Period, Rate_Week.Value);
                        RateList.Add(Answer_Temp);
                    }
                }
                return RateList;
            }
        }
        public List<Rate> ChartWeekXML(string Day_Temp, string CurrencyName)
        {
            return ChartWeek(Day_Temp, CurrencyName);
        }
        public List<Rate> ChartWeekJson(string Day_Temp, string CurrencyName)
        {
            return ChartWeek(Day_Temp, CurrencyName);
        }

        private List<Rate> ChartMonth(string Day_Temp, string CurrencyName)
        {
            /*if (Config)
            {
                EFTracingProviderConfiguration.RegisterProvider();
                EFCachingProviderConfiguration.RegisterProvider();
                EFTracingProviderConfiguration.LogToConsole = false;
                Config = false;
            }
            log4net.Config.DOMConfigurator.Configure();*/

            if (Day_Temp == null)
                return null;
            else
            {
                DateTime DateBegin = Convert.ToDateTime(Day_Temp).AddDays(1 - Convert.ToDateTime(Day_Temp).Day);
                DateTime DateEnd = DateBegin.AddMonths(1);

                //using (ExtendedExtRatesEntities context = new ExtendedExtRatesEntities())
                using (ExtRatesEntities context = new ExtRatesEntities())
                {
                    //context.Log = Log4net;
                    Int32 CurrencyId = context.Currency.Where(c => c.Name == CurrencyName).FirstOrDefault().ID;
                    foreach (var Rate_Month in context.cache_Month.Where(c => c.CurrencyID == CurrencyId).Where(c => c.Period >= DateBegin && c.Period < DateEnd))
                    {
                        Rate Answer_Temp = new Rate(Rate_Month.Period, Rate_Month.Value);
                        RateList.Add(Answer_Temp);
                    }
                }
                return RateList;
            }
        }
        public List<Rate> ChartMonthXML(string Day_Temp, string CurrencyName)
        {
            return ChartMonth(Day_Temp, CurrencyName);
        }
        public List<Rate> ChartMonthJson(string Day_Temp, string CurrencyName)
        {
            return ChartMonth(Day_Temp, CurrencyName);
        }

        private List<Rate> ChartYear(string Day_Temp, string CurrencyName)
        {
            RateList.Clear();

            if (Day_Temp == null)
                return null;
            else
            {
                DateTime DateBegin = Convert.ToDateTime(Day_Temp).AddDays(1 - Convert.ToDateTime(Day_Temp).Day).AddMonths(1 - Convert.ToDateTime(Day_Temp).Month);
                DateTime DateEnd = DateBegin.AddYears(1);
                using (ExtRatesEntities context = new ExtRatesEntities())
                {
                    Int32 CurrencyId = context.Currency.Where(c => c.Name == CurrencyName).FirstOrDefault().ID;
                    foreach (var Rate_Year in context.cache_Year.Where(c => c.CurrencyID == CurrencyId).Where(c => c.Period >= DateBegin && c.Period < DateEnd))
                    {
                        Rate Answer_Temp = new Rate(Rate_Year.Period, Rate_Year.Value);
                        RateList.Add(Answer_Temp);
                    }
                }
                return RateList;
            }
        }
        public List<Rate> ChartYearXML(string Day_Temp, string CurrencyName)
        {
            return ChartYear(Day_Temp, CurrencyName);
        }
        public List<Rate> ChartYearJson(string Day_Temp, string CurrencyName)
        {
            return ChartYear(Day_Temp, CurrencyName);
        }

    }

}
